import Vue from "vue";
import Vuex from "vuex";

Vue.use(Vuex);

export default new Vuex.Store({
  state: {
    Authorization: localStorage.getItem("Authorization"),
    tagsList: [],
    keepLiveList: ["Monitor", "Adinfo"],
    collapse: false, //左侧展开/折叠
    users: {
      usersId: "",
      userName: "",
    }, //监考老师
    name: localStorage.getItem("Namea"),
    infoNumber: null,
    pxswitch: JSON.parse(localStorage.getItem("pxwitch")), // 培训 & 考试 - 模式
    newDate: "", //最新时间
    role: 2, // 权限 1管理员 2普通用户
    usedTime: 0, // 考试用时
  },
  mutations: {
    // 用户姓名
    setusers(state, data) {
      state.users.usersId = data.usersId;
      state.users.userName = data.usersName;
    },
    setNewDate(state, data) {
      state.newDate = data;
    },
    setName(state, val) {
      state.name = val;
    },
    setPxwitch(state, data) {
      state.pxswitch = data;
    },
    setRole(state, data) {
      state.role = data;
    },

    setUsedTime(state, val) {
      state.usedTime = val;
    },
    // 修改token，并将token存入localStorage
    // changeLogin(state, user) {
    //   state.Authorization = user.Authorization;
    //   localStorage.setItem("Authorization", user.Authorization);
    // },
    delTagsItem(state, data) {
      state.tagsList.splice(data.index, 1);
    },
    setTagsItem(state, data) {
      state.tagsList.push(data);
    },
    clearTags(state) {
      state.tagsList = [];
    },
    closeTagsOther(state, data) {
      state.tagsList = data;
    },
    closeCurrentTag(state, data) {
      for (let i = 0, len = state.tagsList.length; i < len; i++) {
        const item = state.tagsList[i];
        if (item.path === data.$route.fullPath) {
          if (i < len - 1) {
            data.$router.push(state.tagsList[i + 1].path);
          } else if (i > 0) {
            data.$router.push(state.tagsList[i - 1].path);
          } else {
            data.$router.push("/");
          }
          state.tagsList.splice(i, 1);
          break;
        }
      }
    },
    // 改变消息条数
    setInfoNumber(state, data) {
      state.infoNumber = data > 0 ? data : "";
    },
    // 侧边栏折叠
    hadndleCollapse(state, data) {
      state.collapse = data;
    },
  },
  actions: {},
  modules: {},
});
